package cn.liumouren.jzxd.controller;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.R;
import cn.liumouren.jzxd.entity.Comment;
import cn.liumouren.jzxd.service.CommentService;
import cn.liumouren.jzxd.utils.QueryUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author Freeman Liu
 * @email llw599502537@gmail.com
 * @date 2020-10-31 00:44:46
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @ApiOperation("万能查询")
    @RequestMapping(value = "/customQuery", method = RequestMethod.GET)
    public R getSceneNames(@RequestParam Map<String, Object> map) {
        PageUtils result = QueryUtil.getResult(commentService, map);
        return R.ok("查询成功", result);
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = commentService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") String id) {
        Comment comment = commentService.getById(id);

        return R.ok().put("data", comment);
    }

    /**
     * 保存
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Comment comment) {
        commentService.save(comment);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R update(@RequestBody Comment comment) {
        commentService.updateById(comment);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody String[] ids) {
        commentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @ApiOperation("分页查询某条动态下的评论")
    @RequestMapping(value = "/getPageCommentByZoneId/{zoneId}/{cur}/{size}", method = RequestMethod.GET)
    public R getPageCommentByZoneId(@PathVariable String zoneId, @PathVariable int cur, @PathVariable int size) {
        IPage<Comment> page = commentService.getPageCommentByZoneId(zoneId, cur, size);
        PageUtils pageUtils = new PageUtils(page);

        return R.ok("成功查询动态[" + zoneId + "]下第[" + cur + "]页(每页显示[" + size + "])评论", pageUtils);
    }

    @ApiOperation("某条动态下的评论数")
    @RequestMapping(value = "/getCommentCount/{zoneId}", method = RequestMethod.GET)
    public R getCommentCountByZoneId(@PathVariable String zoneId) {
        int count = commentService.getCommentCount(zoneId);

        return R.ok("成功查询动态[" + zoneId + "]的评论数", count);
    }

    @ApiOperation("分页查询某条动态下的主评论")
    @RequestMapping(value = "/getMainComment/{zoneId}/{cur}/{size}", method = RequestMethod.GET)
    public R getMainComment(@PathVariable String zoneId, @PathVariable int cur, @PathVariable int size) {
        IPage<Comment> page = commentService.getMainComment(zoneId, cur, size);

        return R.ok("成功查询动态[" + zoneId + "]下第[" + cur + "]页(每页显示[" + size + "])主评论", page);
    }

    @ApiOperation("分页查询某个动态下某个用户被评论的评论")
    @RequestMapping(value = "/getCommentedComment/{zoneId}/{replyUserId}/{cur}/{size}", method = RequestMethod.GET)
    public R getCommentedComment(@PathVariable String zoneId, @PathVariable String replyUserId, @PathVariable int cur, @PathVariable int size) {
        IPage<Comment> page = commentService.getCommentedComment(zoneId, replyUserId, cur, size);

        return R.ok("成功查询动态[" + zoneId + "]下评论用户[" + replyUserId + "]的第[" + cur + "]页(每页显示[" + size + "])评论", page);
    }

    
}
